/**
 *  X86RegisterPool.java : IceBird project
 *  14:31:32 
 */
package icebird.compiler.x86.old;

import icebird.metadata.BasicType;

import java.util.ArrayList;

/**
 * Represents pool of registers.
 * @author Sergey Shulepoff[Knott]
 */
public final class X86RegisterPool {

	private ArrayList<X86Register> stack;
	
	/**
	 * Create register pool.
	 */
	public X86RegisterPool() {
		stack = new ArrayList<X86Register>();
		
		stack.add(X86Register.EAX);
		stack.add(X86Register.EBX);
		stack.add(X86Register.ECX);
		stack.add(X86Register.EDX);
		
		stack.add(X86Register.EDI);
		stack.add(X86Register.ESI);
	}
	
	/**
	 * Requests register from pool.
	 * @return X86Register
	 */
	public X86Register request(BasicType type) {
		if (type.isWide()) {
			return null;
		}
		
		int size = stack.size();
		if (size == 0) {
			return null;
		} else {
			return stack.remove(size - 1);
		}
	}
	
	/**
	 * Release x86 register.
	 * @return void
	 */
	public void release(X86Register reg) {
		stack.add(reg);
	}
}
